package sealte.wdgf.system.listener;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class EAIServerSessionListener implements HttpSessionListener {
	
	private static Logger logger = LoggerFactory.getLogger(EAIServerSessionListener.class);

	@SuppressWarnings("unchecked")
	@Override
	public void sessionCreated(HttpSessionEvent hse) {
		HttpSession session = hse.getSession();
		ServletContext application = session.getServletContext();
		String sessionId = session.getId();
		if(session.isNew()){
			Map<String, List<HttpServletRequest>> rom = 
					(Map<String, List<HttpServletRequest>>)application.getAttribute("ROM");
			List<HttpServletRequest> requestList = new ArrayList<HttpServletRequest>();
			rom.put(sessionId, requestList);
			logger.info("sessionId:"+sessionId+" join in ROM!");
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public void sessionDestroyed(HttpSessionEvent hse) {
		HttpSession session = hse.getSession();
		ServletContext application = session.getServletContext();
		String sessionId = session.getId();
		Map<String, List<HttpServletRequest>> rom = 
				(Map<String, List<HttpServletRequest>>)application.getAttribute("ROM");
		rom.remove(sessionId);
		logger.info("sessionId:"+sessionId+" is removed from ROM!");
	}
	
}
